<template>
  <div className="goods-comment" v-if="commentInfo">
    <div className="head">
      <div className="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div className="tags">
        <div className="dt">大家都在说：</div>
        <!--        筛选条件-->
        <div className="dd">
          <a
            href="javascript:"
            v-for="item in commentInfo.tags"
            :key="item.title"
            :class="{ active: item.title === activeTagName }"
            @click="
              activeTagName = item.title;
              updateParams({ tag: item.title });
            "
            >{{ item.title }}（{{ item.tagCount }}）</a
          >
        </div>
      </div>
    </div>
    <div className="sort">
      <span>排序：</span>
      <a
        href="javascript:"
        :class="{ active: params.sortField === '' }"
        @click="updateParams({ sortField: '' })"
        >默认</a
      >
      <a
        href="javascript:"
        :class="{ active: params.sortField === 'createTime' }"
        @click="updateParams({ sortField: 'createTime' })"
        >最新</a
      >
      <a
        href="javascript:"
        :class="{ active: params.sortField === 'praiseCount' }"
        @click="updateParams({ sortField: 'praiseCount' })"
        >最热</a
      >
    </div>
    <div className="list" v-if="commentList">
      <div class="item" v-for="item in commentList.items" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ formatNickName(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <!--星星-->
          <div class="score">
            <i
              class="iconfont icon-wjx01"
              v-for="score in item.score"
              :key="'s' + score"
            ></i>
            <i
              class="iconfont icon-wjx02"
              v-for="score in 5 - item.score"
              :key="'k' + score"
            ></i>
            <!--规格-->
            <span class="attr">{{ formatAttrs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">
            {{ item.content }}
          </div>
          <!--          渲染图片图片-->
          <GoodsCommentImage
            v-if="item.pictures.length > 0"
            :pictures="item.pictures"
          />
          <div class="time">
            <span>2020-10-10 10:11:22</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i
              >{{ item.orderInfo.createTime }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <XtxPagination
      v-model:page="params.page"
      :pageSizes="params.pageSize"
      :counts="counts"
    />
  </div>
</template>
<script>
import { ref, watch } from "vue";
import { getCommentInfo, getCommentList } from "../../../api/goods";
import { onBeforeRouteUpdate, useRoute } from "vue-router";
import GoodsCommentImage from "./GoodsCommentImage";
import XtxPagination from "../../../components/library/XtxPagination";

export default {
  name: "GoodsComment",
  components: { XtxPagination, GoodsCommentImage },
  setup() {
    const {
      commentInfo,
      getData: getInfo,
      activeTagName,
    } = useGoodsCommentInfo();
    // 发起请求
    getInfo();
    const {
      commentList,
      getData: getList,
      formatAttrs,
      updateParams,
      params,
      counts,
    } = useGoodsCommentList();
    getList();

    // 格式化昵称
    const formatNickName = (nickname) => {
      // 截取第一个    加入*** 再截取最后一个
      return nickname.substr(0, 1) + "***" + nickname.substr(-1);
    };

    // 在路由切换时重新获取评价数据
    onBeforeRouteUpdate((to) => getInfo(to.params.id));
    return {
      commentInfo,
      commentList,
      activeTagName,
      formatNickName: formatNickName,
      formatAttrs,
      updateParams,
      params,
      counts,
    };
  },
};
// 获取评价头部数据
function useGoodsCommentInfo() {
  // 用于存储组件信息
  const commentInfo = ref();
  // 获取路由信息对象
  const route = useRoute();
  // 用于实现标签的选中效果
  const activeTagName = ref("全部评价");
  // 发起请求
  const getData = () => {
    // 获取数据
    getCommentInfo(route.params.id).then((data) => {
      data.result.tags.unshift({
        tagCount: 50,
        title: "有图",
      });

      data.result.tags.unshift({
        tagCount: `999+`,
        title: "全部评价",
      });

      // 储存数据
      commentInfo.value = data.result;
    });
  };
  // 返回数据
  return {
    commentInfo,
    getData,
    activeTagName,
  };
}

// 获取评价列表数据
function useGoodsCommentList() {
  // 用于储存列表数据
  const commentList = ref();
  // 获取路由信息对象
  const route = useRoute();
  // 参数对象
  const params = ref({
    page: 1,
    pageSize: 10,
    hasPicture: false,
    tag: "",
    sortField: "",
  });
  // 更新params 筛选条件
  const updateParams = (target) => {
    if (target.tag) {
      params.value = {
        ...params.value,
        // ...target,
        hasPicture: target.tag === "有图",
        tag:
          target.tag === "全部评价" || target.tag === "有图" ? "" : target.tag,
      };
    } else {
      params.value = {
        ...params.value,
        ...target,
      };
    }
  };
  const formatAttrs = (attrs) => {
    return attrs.map((item) => {
      return `${item.name} : ${item.nameValue}`;
    });
  };
  // 给分页页面存储总页数
  const counts = ref(0);
  const getData = () => {
    getCommentList(route.params.id, params).then((data) => {
      commentList.value = data.result;
      counts.value = data.result.counts;
    });
  };

  // 监听params变化
  watch(
    () => params.value,
    () => {
      // 重新发送请求获取评价列表数据
      getData();
    },
    {
      immediate: true,
      deep: true,
    }
  );

  return {
    commentList,
    getData,
    formatAttrs,
    updateParams,
    params,
    counts,
  };
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;

    .data {
      width: 340px;
      display: flex;
      padding: 20px;

      p {
        flex: 1;
        text-align: center;

        span {
          display: block;

          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }

          &:last-child {
            color: #999;
          }
        }
      }
    }

    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;

      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }

      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;

        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;

          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }

          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }

  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;

    > span {
      margin-left: 20px;
    }

    > a {
      margin-left: 30px;

      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }

  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
